<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class RCMS_Object_Page_PageModel extends RCMS_Core_BaseModel
{
    protected $_name = 'page';

/**
 * @method This method make a select page data by pageID
 * @param <RCMS_Object_Page_Page> $pageObj
 * @return <Integer>
 */
    public function selectPageDataById($pageId)
    {
        if($pageId>0){
            $select = $this->getAdapter()->select()->from($this->_name)->where("id = ?",$pageId)->limit(1);
            $stmt   = $this->getAdapter()->query($select);
            $result = $stmt->fetchAll();
            if($result)
            return $result;
            return null;
        }
        else
        {
            return null;
        }

    }


	public function getParentCatId($id) {
		$select = $this->getAdapter()->select()
			->from(
				array('c' => 'category'),
				array(
					'parentId' => 'id_parent',
				)
			)
			->where('c.id=?', $id);
		return $this->_adapter->fetchRow($select, null, Zend_Db::FETCH_ASSOC);
	}

	public function selectParentCategoryName($parentCatId) {
		$select = $this->getAdapter()->select()
			->from(
				array('c' => 'category'),
				array(
					'name'
				)
			)
			->where('c.id=?', $parentCatId);
		return $this->_adapter->fetchRow($select, null, Zend_Db::FETCH_ASSOC);
	}


/**
 * @method This method is inserting new page
 * @param <RCMS_Object_Page_Page> $pageObj
 * @return <Integer> last insert id
 */
    public function inserPage(RCMS_Object_Page_Page $pageObj)
    {
        $pageData = array(
             'h1'=>$pageObj->getH1Tag(),
             'header_title'=>$pageObj->getHeaderTitle(),
             'url'=> urlencode($pageObj->getUrl()),
             'nav_name'=>$pageObj->getNavName(),
             'meta_description'=>$pageObj->getMetaDescription(),
             'meta_keywords'=>$pageObj->getMetaKeywords(),
             'short_description'=>$pageObj->getShortDescription(), 
             'is_404page'=>$pageObj->getIs404Page(),
             'template_id'=>$pageObj->getTemplateId(),
             'category_id'=>$pageObj->getCategoryId(),
             'bestpractices'=>$pageObj->getBestPractices(),
             'date'=>$pageObj->getDate(),
             'owner'=>$pageObj->getOwner(),
             'last_update'=>$pageObj->getLastUpdate(),
             'date'=>date("Y-m-d H:i:s"),
             'show_in_menu'=>$pageObj->getShowInMenu(),
			 'order' => $pageObj->getOrder(),
			 'static_order' => $pageObj->getStaticOrder(),
             'weight' => $pageObj->getWeight(),
			 'seosamba_rule_id' => $pageObj->getSeosambaRuleId(),
			 'silo_id' => $pageObj->getSiloId(),
			 'targeted_key_phrase' => $pageObj->getTargetedKeyPhrase()
        );
        $this->getAdapter()->insert($this->_name, $pageData);
        $result = $this->getAdapter()->lastInsertId($this->_name);
        return $result;
    }
/**
 * @method This method is updating page
 * @param <RCMS_Object_Page_Page> $pageObj
 * @return <Integer>
 */
    public function updatePage(RCMS_Object_Page_Page $pageObj)
    {
      $pageData = array(
             'h1'=>$pageObj->getH1Tag(),
             'header_title'=>$pageObj->getHeaderTitle(),
             'url'=> urlencode($pageObj->getUrl()),
             'nav_name'=>$pageObj->getNavName(),
             'meta_description'=>$pageObj->getMetaDescription(),
             'meta_keywords'=>$pageObj->getMetaKeywords(),
             'short_description'=>$pageObj->getShortDescription(),
             'is_404page'=>$pageObj->getIs404Page(),
	         'template_id'=>$pageObj->getTemplateId(),
             'category_id'=>$pageObj->getCategoryId(),
             'bestpractices'=>$pageObj->getBestPractices(),
             'date'=>$pageObj->getDate(),
             'owner'=>$pageObj->getOwner(),
             'last_update'=>date("Y-m-d H:i:s"),
             'show_in_menu'=>$pageObj->getShowInMenu(),
			 'order' => $pageObj->getOrder(),
			 'static_order' => $pageObj->getStaticOrder(),
             'weight' => $pageObj->getWeight(),
			 'seosamba_rule_id' => $pageObj->getSeosambaRuleId(),
			 'silo_id' => $pageObj->getSiloId(),
			 'targeted_key_phrase' => $pageObj->getTargetedKeyPhrase()
        );
     $result = $this->getAdapter()->update($this->_name, $pageData,"id = ".$pageObj->getId());
     return $result;
     
        
    }

/**
 * @method This method delete page in the database
 * @param <RCMS_Object_Page_Page> $pageObj
 * @return <Integer>
 */
    public function deletePage(RCMS_Object_Page_Page $pageObj)
    {
        return $this->getAdapter()->delete($this->_name,"id = ".$pageObj->getId());
    }

	public function setParentCategory($pageId, $parentCatId) {
		$contentData = array(
			'id_parent' => $parentCatId,
		);
		$contentWhere = sprintf("id = %d", $pageId);
		return $this->_adapter->update('category', $contentData, $contentWhere);
	}

    /**
 * @method This method is updating temnplate_id to default template
 * @param <RCMS_Object_Page_Page> $pageObj
 * @return <Integer>
 */
    public function updateToDefaultTemplate($idDeletedTemplate)
    {
        $select = $this->getAdapter()->select()->from('template',array('id'))->where('name = "default"');
        $stmt   = $this->getAdapter()->query($select);
        $result = $stmt->fetchColumn();
              
        $pageData = array(
             'template_id'=>$result
        );
     $result = $this->getAdapter()->update($this->_name, $pageData,"template_id = ".$idDeletedTemplate);
     return $result;


    }

    public function deleteImages($pageId)
    {
       return $this->getAdapter()->delete("page_image","page_id = ".$pageId);
    }


}

